package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.DeviceAuthResultInfo;
import com.colorlight.cloud.complete.entity.po.SenderAuthResultInfo;
import com.colorlight.cloud.complete.entity.po.Time;
import com.colorlight.cloud.grandshow.entity.po.AuthorizationDetail;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 定时加密
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class TimingEncrypt implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        /**
         * 时间
         */
        private Time time;
        private AuthorizationDetail authorizationDetail;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        /**
         * 设备授权结果信息
         */
        private DeviceAuthResultInfo deviceAuthResultInfo;
        /**
         * 发送器授权结果信息列表
         */
        private List<SenderAuthResultInfo> senderAuthResultInfo;
    }

    private InputMsg input;
    private OutputMsg output;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.TimingEncrypt.InputMsg.Builder inputMsg =
                CompleteInstruction.TimingEncrypt.InputMsg.newBuilder();
        TimingEncrypt.InputMsg thisInput = this.getInput();
        inputMsg.setTime(thisInput.getTime().toProto());
        final CompleteInstruction.TimingEncrypt.Builder builder =
                CompleteInstruction.TimingEncrypt.newBuilder();
        builder.setInput(inputMsg);
        return builder.build().toByteString();
    }

    @Override
    public TimingEncrypt byteConvertObject(ByteString bytes) {
        CompleteInstruction.TimingEncrypt timingEncrypt = null;
        try {
            timingEncrypt = CompleteInstruction.TimingEncrypt.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        if (Objects.isNull(timingEncrypt)) {
            throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
        }
        TimingEncrypt.OutputMsg outputMsg = new TimingEncrypt.OutputMsg();
        CompleteInstruction.TimingEncrypt.OutputMsg output = timingEncrypt.getOutput();
        DeviceAuthResultInfo deviceAuthResultInfo = new DeviceAuthResultInfo().formProto(output.getDeviceAuthResultInfo());
        List<SenderAuthResultInfo> senderAuthResultInfo = output.getSenderAuthResultInfoList().stream()
                .map(proto -> new SenderAuthResultInfo().formProto(proto))
                .collect(Collectors.toList());
        outputMsg.setDeviceAuthResultInfo(deviceAuthResultInfo);
        outputMsg.setSenderAuthResultInfo(senderAuthResultInfo);
        this.setOutput(outputMsg);
        return this;
    }
}

